<?php

session_start();

$customer_id = $_SESSION['userid'];

$search_string = $_POST['searched_item'];

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="SHORTCUT ICON" href="images/url_logo.png">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Buy secondhand horse saddles online with our large range of horse saddles for most disciplines including General Purpose | Dressage | Eventing | Working | Hunter and VSD. Buy or sell used horse saddles online.">

<title>24-7 Saddles | Search results</title>

<!-- Website tracking -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-54780113-1', 'auto');
  ga('send', 'pageview');

</script>
<!-- Website tracking -->

<link href="css/header.css" rel="stylesheet" type="text/css">
<link href="css/navigation.css" rel="stylesheet" type="text/css" />
<link href="css/search_results.css" rel="stylesheet" type="text/css" />
<link href="css/products_product.css" rel="stylesheet" type="text/css" />

<style type="text/css">
body {
	background-image: url(images/wood2.jpg);
	background-repeat: repeat-x;
	margin: 0px;
	background-position: left top;
	background-attachment: fixed;
	background-color: #000000;
	padding-top: 0px;
	padding-right: 0px;
	padding-bottom: 0px;
	padding-left: 0px;
}
</style>
</head>

<body>

<div id="page_container">

<?php include "header.php" ?>

<?php include "navigation.php" ?>

<div id="title_container">

<h1>Your search results...</h1>
<h2>You searched <?php echo "<b>".$search_string."</b>" ?></h2>

</div>

<img id="saddle_makes" src="images/saddle_makes.png" width="980" height="60" />

<div id="new_products_container"> <!--New Products Container-->

<ul>

<?php

	include('connect.php');
	
	explode(" ",$search_string);
	
	$array = explode(" ",$search_string);;
	
	$search_string_1 = $array[0];
	$search_string_2 = $array[1];
	
	/////////////////////////
	//Testing - Write queries
	/////////////////////////
	
	//print_r($array)."<br />";
	//echo $search_string_1."<br />";
	//echo $search_string_2."<br />";
	//echo $query;
	
	/////////////////////////
	//END Testing - Write queries
	/////////////////////////
	
	$search_results = array();

//For each array value search for it within saddles
foreach ($array as &$value) {
	
	$query = "SELECT * FROM saddles WHERE product_name LIKE '%$value%' OR product_colour LIKE '%$value%' AND product_status != 'Sold' ";
	$result = mysql_query($query);
	
	while($row = mysql_fetch_array($result))
	 
	 {		 
		 
	$product_id = $row['product_id'];
	
	array_push($search_results, $product_id);
	
	 }
	
}
	
//print_r($search_results);
	
	
	ksort($search_results);	
	$number_results = (count(array_unique($search_results)));

    /////////////////////////
	//Testing - Write queries
	/////////////////////////

		//print_r(array_count_values($search_results));
		//print_r(count(array_unique($search_results))); //Number of unique returened results
		//print_r($search_results);

    /////////////////////////
	//END Testing - Write queries
	/////////////////////////
	
///////////////////////////////////////////////////
//Record search results in the search_results table
///////////////////////////////////////////////////

		$today = date("Y/m/d");
		$ip_address = $_SERVER['REMOTE_ADDR'];
		$time = date("H:i:s"); 

		mysql_query("INSERT INTO search_history 

		(
		
		search_string,
		search_date,
		search_time,
		number_results,
		ip_address
			
		) 
		
		VALUES
		
		(
		
		'$search_string',
		'$today',
		'$time',
		'$number_results',
		'$ip_address')
	
	 	"); 

////////////////////
//End record results
////////////////////
	
$search_results = array_unique($search_results);

foreach ($search_results as $value) {
	
	$query = "SELECT * FROM saddles WHERE product_id = '$value' ";
	$result = mysql_query($query);
	
	while($row = mysql_fetch_array($result))
	 
	 {		 
		 
	$product_name = $row['product_name'];
	$product_status = $row['product_status'];	
	$product_cost = $row['product_cost']; 
	$product_description = $row['product_description'];
	$product_id = $row['product_id'];
	$product_row_id = $row['id'];
	$product_offer = $row['product_offer'];
	$product_link = preg_replace('/\s+/', '_', $product_name);
	
	echo '<li>';
	echo '<form action="product_details.php?'.$product_name.'_second_hand_horse_saddles" method="post" name="product">';
	echo '<img alt="24-7saddles second hand horse saddle '.$product_name.'" id="product_imgage_large" src="images/saddles/'.$product_row_id.'_1.jpg" width="500" height="500" />';
	echo '<a href="saddles/'.$product_link.'.php" class="product_button">More details</a>';
	echo '<h4>'.$product_name.'</h4>';
	
	if($product_status == "On-trial") //if saddle is on trial
	{
	echo '<p id="ontrial_banner">On-trial</p>';
	}
	
	if($product_status != "On-trial") //if saddle isnt on trial
	{
		if($product_offer != "") //if saddle has is on offer
		{
			echo '<p id="product_banner">'.$product_offer.'</p>';
		}
		
		if($product_offer == "") //if saddle isnt on offer
		{
			echo '';
		}
	}
	
	echo '<p id="product_price"><i>Only</i> £'.$product_cost.'</p>';
	echo '<p id="product_description">'.$product_description.'</p>';
	//echo '<input name="product_row_id" type="hidden" value="'.$product_row_id.'" />';
	//echo '<input name="view_product" type="submit" class="product_button" value="More details" />';
	
	echo '</form>';
		
			
	echo '<p id="product_id"><i>Product ID: '.$product_id.'</i></p>';
	echo '<li>';
	

	
	 }
	
}

?>

</ul>

</div> 
<!--End Intro Container-->

</div> 
<!--page_container-->

<?php include "footer.php" ?>

</body>
</html>